package 最接近的三数之和;

public class Solution {

    public static void main(String[] args) {
        int[] nums = new int[]{1,1,-1,-1,3};
        int target = -1;

        Solution solution = new Solution();

        System.out.println(solution.threeSumClosest(nums,target));

    }

    public int threeSumClosest(int[] nums, int target) {
        int size = nums.length;
        int result = nums[0] + nums[1] + nums[2];
        for (int i = 0; i < size; i++) {
            int dataI = nums[i];
            for (int j = i+1; j < size; j++) {
                int dataJ = nums[j];
                for (int k = j+1; k < size; k++) {
                    int dataK = nums[k];
                    int sum = dataI + dataJ + dataK;
                    if(Math.abs(target - sum) < Math.abs(target - result)){
                        result = sum;
                    }
                }
            }
        }

        return result;
    }

}
